import os
import datetime
import json

from catalog.models.sentinel2_l2a import Sentinel2L2AScene


def quick_test():
    dir_catalog = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    fpath_item = os.path.join(
        dir_catalog,
        "schemas",
        "examples",
        "COPERNICUS",
        "SENTINEL2",
        "L2A",
        "48",
        "48RVV",
        "S2B_MSIL2A_20200906T034539_N0214_R104_T48RVV_20200906T071603.SAFE.json",
    )
    info_item = json.load(open(fpath_item))
    print(info_item)

    # n = Sentinel2L2AScene.sweep_deleted_items(dry_run=False)
    # print(f"{n} items deleted")

    # check if an item exist or not
    item = Sentinel2L2AScene.get_one_item(
        "S2B_MSIL2A_20200906T034539_N0214_R104_T48RVV_20200906T071603"
    )
    if item is not None:
        print(item)
        print(item.transferred)
        item.transferred = datetime.datetime.utcnow()
        print(item.transferred)
        print(item.is_dirty())
        print(type(item.geom))
        ret = item.save()
        print(ret)
    else:
        print("no such a record or already deleted")
        print("insert such an item")
        inserted = Sentinel2L2AScene.insert_one_item(info_item, on_conflict="update")
        print(inserted)

        # check that item again
        item = Sentinel2L2AScene.get_one_item(
            "S2B_MSIL2A_20200906T034539_N0214_R104_T48RVV_20200906T071603"
        )
        print(item)

    print("exist", item.uid, item.valid_until)
    #    # delete that item (actually just setting the valid_until to the current datetime)
    #    res = Sentinel2L2AScene.delete_one_item(item.uid)
    #    print(res)


if __name__ == "__main__":
    import fire

    fire.Fire(
        {
            "quick-test": quick_test,
        }
    )
